package com.ugrow.ugrowcommunity.activity.seatReservation.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Button;
import android.widget.TextView;

import com.ugrow.ugrowcommunity.R;


public class ReservationSuccessDialog extends Dialog {
    private String start;
    private String end;
    private String classroom;
    private String seat;
    private final boolean canCancel;
    private TextView startText;
    private TextView classroomText;
    private TextView endText;
    private TextView seatText;
    private TextView time;
    private int num;

    public ReservationSuccessDialog(Context context, String start, String end, String classroom,String seat, boolean canCancel) {
        super(context, R.style.LoadProgressDialog);
        this.start = start;
        this.end = end;
        this.seat = seat;
        this.classroom = classroom;
        this.canCancel = canCancel;
    }

    public void setTime(String start, String end) {
        this.start = start;
        this.end = end;
        handler.sendEmptyMessage(0);
    }

    public void setClassroom(String classroom) {
        this.classroom = classroom;
        handler.sendEmptyMessage(0);
    }

    public void setSeat(String seat) {
        this.seat = seat;
        handler.sendEmptyMessage(0);
    }

    @SuppressLint("HandlerLeak")
    private final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                startText.setText(start);
                endText.setText(end);
                classroomText.setText(classroom);
                seatText.setText(seat);
            }
        }
    };

    @SuppressLint("ResourceType")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_msg_dialog);
        startText = findViewById(R.id.start_time);
        endText = findViewById(R.id.end_time);
        classroomText = findViewById(R.id.classroom);
        seatText = findViewById(R.id.seat_num);
        setCanceledOnTouchOutside(canCancel);
        startText.setText(start);
        endText.setText(end);
        classroomText.setText(classroom);
        seatText.setText(seat);
        time = findViewById(R.id.time);
        Button button = findViewById(R.id.button);

        new TimeThread().start();
        num = 15;
        button.setOnClickListener(v -> dismiss());
    }

    private class TimeThread extends Thread {
        @Override
        public void run() {
            do {
                try {
                    Thread.sleep(1000);
                    Message msg = new Message();
                    msg.what = 1;  //消息(一个整型值)
                    mHandler.sendMessage(msg);// 每隔1秒发送一个msg给mHandler
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } while (true);
        }
    }

    //在主线程里面处理消息并更新UI界面
    @SuppressLint("HandlerLeak")
    private final Handler mHandler = new Handler() {
        @SuppressLint("SetTextI18n")
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                num--;
                time.setText(num + ""); //更新时间
                if (num == 0) {
                    dismiss();
                }
            }
        }
    };
}

